package com.zpkj.portal.service.user.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zpkj.portal.dto.ResponseParameter;
import com.zpkj.portal.entity.UserMain;
import com.zpkj.portal.service.user.UserMainService;
import com.zpkj.portal.service.user.UserService;
import com.zpkj.portal.utils.Constant;

/**
 * @author Kelvin
 */
@Service
@Transactional
@ConfigurationProperties(prefix = "alioss")
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMainService userMainService;

	/* (non-Javadoc)
	 * @see com.zpkj.portal.service.user.UserService#systemRegister(java.lang.String, java.lang.String, java.lang.String, java.lang.String, com.zpkj.portal.dto.ResponseParameter, java.lang.String)
	 */
	@Override
	@Transactional
	public String systemRegister(String tel, String pwd, String smsCode, String inviteCode, ResponseParameter result,
			String appType) {
		return null;
	}

	/* (non-Javadoc)
	 * @see com.zpkj.portal.service.user.UserService#systemUserRegister(java.lang.String, java.lang.String, java.lang.String, java.lang.String, com.zpkj.portal.dto.ResponseParameter, java.lang.String)
	 */
	@Override
	@Transactional
	public String systemUserRegister(String tel, String pwd, String inviteCode, String redisKey,
			ResponseParameter result, String appType) {
		return null;
	}

	/* (non-Javadoc)
	 * @see com.zpkj.portal.service.user.UserService#updatePwd(java.lang.String, java.lang.String, java.lang.String, com.zpkj.portal.dto.ResponseParameter)
	 */
	@Override
	@Transactional
	public String updatePwd(String loginUserId, String oldPwd, String newPwd, ResponseParameter result) {
		// 检测手机是否已注册
		UserMain userMain = userMainService.getUserMainByUserId(loginUserId);
		if (userMain == null) {
			return result.addError(Constant.ReturnCode.CODE_1110, "用户不存在").toJson();
		}
		// 检测原密码是否正确
		String oldPwd_db = userMain.getPassword();
		if (!oldPwd.equals(oldPwd_db)) {
			return result.addError(Constant.ReturnCode.CODE_1110, "原密码不正确,如忘记请找回密码").toJson();
		} else {
			// -----修改密码-----
			userMain.setPassword(newPwd);
			userMainService.updateUser(userMain);
		}
		return result.setResultCode(Constant.ReturnCode.CODE_1000).toJson();
	}

	/* (non-Javadoc)
	 * @see com.zpkj.portal.service.user.UserService#systemLogin(java.lang.String, java.lang.String, com.zpkj.portal.dto.ResponseParameter, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public String systemLogin(String tel, String pwd, ResponseParameter result, String ipAdds, String macNo,
			String appType) {
		// 检测手机是否已注册
		UserMain userMain = userMainService.getUserIdByTel(tel);
		if (userMain == null) {
			return result.addError(Constant.ReturnCode.CODE_2102, "手机号未注册,请先注册").toJson();
		}

		Map<String, String> retMap = null;
		return result.addData(retMap).setResultCode(Constant.ReturnCode.CODE_1000).toJson();
	}

	/* (non-Javadoc)
	 * @see com.zpkj.portal.service.user.UserService#smsCodeLogin(java.lang.String, java.lang.String, com.zpkj.portal.dto.ResponseParameter, java.lang.String, java.lang.String)
	 */
	@Override
	public String smsCodeLogin(String tel, String smsCode, ResponseParameter result, String inviteCode,
			String appType) {
		return null;
	}

	/* (non-Javadoc)
	 * @see com.zpkj.portal.service.user.UserService#logout(java.lang.String)
	 */
	@Override
	@Transactional
	public String logout(String userId) {
		UserMain userMain = userMainService.getUserMainByUserId(userId);
		userMain.setLoginFlag(1);// 是否在线:0是 1否
		userMainService.updateUser(userMain);
		// modify end
		return Constant.ReturnCode.CODE_1000;
	}

}
